﻿Public Class tAlumno
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        divCalendario.Visible = False
        lblMsg.Text = ""
    End Sub

    Protected Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        lblMsg.Text = ""

        If Not Page.IsValid Then
            lblMsg.Text = "Datos invalidos"
            Return
        End If

        Dim tAlumno As LocalDataTableAdapters.AlumnosTableAdapter
        Dim rAlumnos As LocalData.AlumnosDataTable
        Try
            tAlumno = New LocalDataTableAdapters.AlumnosTableAdapter
            rAlumnos = New LocalData.AlumnosDataTable
        Catch ex As Exception
            lblMsg.Text = "Error al acceder a la base de datos"
            Return
        End Try
        
        Dim dni As Integer

        If Not Integer.TryParse(txtDNI.Text, dni) Then
            lblMsg.Text = "Numero de documento incorrecto"
            Return
        End If

        If dni < 6000000 Then
            lblMsg.Text = "Numero de documento incorrecto."
            Return
        End If

        Dim FechaNac As Date

        If Not Date.TryParse(txtFechaNac.Text, FechaNac) Then
            lblMsg.Text = "Fecha de nacimiento invalida"
            txtFechaNac.Focus()
            Return
        End If

        Try
            tAlumno.Fill(rAlumnos)

            Dim s() As DataRow = rAlumnos.Select("dni = " & dni.ToString)

            If s.Length > 0 Then
                lblMsg.Text = "El numero de documento esta repetido"
                Return
            End If


            rAlumnos.AddAlumnosRow(dni, txtApellido.Text, txtNombre.Text, txtSexo.Text, FechaNac, txtNacionalidad.Text, txtDomicilio.Text, _
                                   txtLocalidad.Text, txtPais.Text, txtTelefono.Text, txtEMail.Text)
            tAlumno.Update(rAlumnos)
        Catch ex As Exception
            lblMsg.Text = "Error al actualizar los datos: " & ex.Message
            Return
        End Try
    End Sub

    Protected Sub btnCalendario_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles btnCalendario.Click
        divCalendario.Visible = True
    End Sub


    Protected Sub cal_SelectionChanged(sender As Object, e As EventArgs) Handles cal.SelectionChanged
        txtFechaNac.Text = cal.SelectedDate.ToShortDateString
    End Sub

    Protected Sub btnCerraCal_Click(sender As Object, e As EventArgs) Handles btnCerraCal.Click
        divCalendario.Visible = False
    End Sub
End Class